//
//  NSString+XKMD5.m
//  24_加密
//
//  Created by 徐康 on 16/8/16.
//  Copyright © 2016年 KX. All rights reserved.
//

#import "NSString+XKMD5.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (XKMD5)
- (NSString *)stringWith32BitMD5Lower {
    
    return [[self stringWith32BitMD5]lowercaseString];;
}
- (NSString *)stringWith32BitMD5 {
    // 1,将字符串转换成C语言字符串
    const char *cString = [self UTF8String];
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    //将C语言字符串转换成 MD5
    CC_MD5(cString,length,bytes);
    //3.将C的字符串转换成OC的字符串
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [string appendFormat:@"%02x",bytes[i]];
    }
    return string;

}
- (NSString *)stringWith32BitMD5Upper {
    return [[self stringWith32BitMD5] uppercaseString];
}
- (NSString *)stringWith16BitMD5 {
    NSString *mdString32Bit = [self stringWith32BitMD5];
    NSString *mdString16Bit = [mdString32Bit substringToIndex:mdString32Bit.length - 8];
    mdString16Bit = [mdString16Bit substringFromIndex:8];
    return mdString16Bit;
}

- (NSString *)stringWith16BitMD5Lower {
    return [[self stringWith16BitMD5] lowercaseString];
}
- (NSString *)stringWith16BitMD5Upper {
    return [[self stringWith16BitMD5] uppercaseString];
}

@end
